#ifndef OPTIONSPERSISTER
#define OPTIONSPERSISTER

#include <string>
#include <fstream>
#include <ShlObj.h>
#include "../WinMacroLib/WinMacroLib.h"

struct UIOPTIONS
{
	WORD recording_hotkey_modifier;
	WORD recording_hotkey_virtualkey;
	WORD playback_hotkey_modifier;
	WORD playback_hotkey_virtualkey;
	std::string most_recent_macro_file;
	bool shutdown_after_playback;
};

class OptionsPersister
{
public:
	void ReadOptions(RECORDINGOPTIONS &recording_options, PLAYBACKOPTIONS &playback_options, UIOPTIONS &ui_options)
	{
		const std::string file_path = GetOptionsFilePath();

		std::ifstream in(file_path.c_str());

		if (in.fail())
		{
			playback_options.normal_speed = true;
			playback_options.repeat_count = 0;
			playback_options.user_defined_time_interval = 0;
			playback_options.add_to_recorded_time = false;
			recording_options.ignore_mouse_actions = false;

			ui_options.recording_hotkey_modifier = MOD_CONTROL | MOD_SHIFT;
			ui_options.recording_hotkey_virtualkey = VK_F4;

			ui_options.playback_hotkey_modifier = MOD_CONTROL | MOD_SHIFT;
			ui_options.playback_hotkey_virtualkey = VK_F5;
		}
		else
		{
			in >> recording_options.ignore_mouse_actions;
			in >> playback_options.add_to_recorded_time >> playback_options.normal_speed >> playback_options.repeat_count >> playback_options.user_defined_time_interval;
			in >> ui_options.recording_hotkey_virtualkey >> ui_options.recording_hotkey_modifier;
			in >>  ui_options.playback_hotkey_virtualkey >> ui_options.playback_hotkey_modifier;

			std::string emptyline;
			std::getline(in, emptyline);
			std::getline(in, ui_options.most_recent_macro_file);

			if (!in.eof())
			{
				in >> ui_options.shutdown_after_playback;
			}
		}
	}

	void WriteOptions(const RECORDINGOPTIONS &recording_options, const PLAYBACKOPTIONS &playback_options, const UIOPTIONS &ui_options)
	{
		const std::string file_path = GetOptionsFilePath();	

		std::ofstream out(file_path.c_str(), std::ios::out | std::ios::trunc);

		out << recording_options.ignore_mouse_actions << std::endl;
		out << playback_options.add_to_recorded_time  << std::endl << playback_options.normal_speed << std::endl << playback_options.repeat_count << std::endl << playback_options.user_defined_time_interval << std::endl;
		out << ui_options.recording_hotkey_virtualkey  << std::endl << ui_options.recording_hotkey_modifier << std::endl;
		out << ui_options.playback_hotkey_virtualkey  << std::endl << ui_options.playback_hotkey_modifier << std::endl;
		out << ui_options.most_recent_macro_file << std::endl;
		out << ui_options.shutdown_after_playback;
	}

private:
	const std::string GetOptionsFilePath()
	{
		char buf[MAX_PATH] = {};
		std::string file_path = "Options";

		if (SUCCEEDED(
			SHGetFolderPath(NULL,
			CSIDL_APPDATA,
			NULL,
			0,
			buf)
			))
		{
			std::string dir_path = std::string(buf) + "\\WinMacro\\";
			if (SUCCEEDED(CreateDirectory(dir_path.c_str(), NULL)))
			{
				file_path = dir_path + "Options";
			}
		}

		return file_path;
	}
};

#endif